//
//  HMEmoticonKeyboardView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/16.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

let EMJOCOLLECTIONCELL = "EMJOCOLLECTIONCELL"



class HMEmoticonKeyboardView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 通过 indexPath 设置 pageControl当前显示的总页数和当前选中的页
    private func setupPageControl(indexPath: NSIndexPath) {
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
        // 当是第0组的时候,就隐藏pageControl
        pageControl.hidden = indexPath.section == 0
    }

    
    private func setupUI() {
        
        // 图片当背景
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)

        addSubview(emoticonToolBar)
        addSubview(collectionView)
        addSubview(pageControl)
        
        emoticonToolBar.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(self)
        }
        
        collectionView.snp_makeConstraints { (make) in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        // 注册cell
        collectionView.registerClass(HMEmjoCell.self, forCellWithReuseIdentifier: EMJOCOLLECTIONCELL)
        
    }
    
    // MARK: -懒加载
    private lazy var emoticonToolBar: HMEmoticonToolBar = {
        
        let em = HMEmoticonToolBar(frame: CGRectZero)
        
        em.delegate = self
        
        return em
        
    }()
    
    private lazy var collectionView: HMEmjoCollectionView = {
        
        let con = HMEmjoCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        con.dataSource = self
        con.delegate = self
    
        return con
    }()
    
    
    // 分隔符
    private lazy var pageControl: UIPageControl = {
       
        let pageControl = UIPageControl()
        // 关闭用户交互
        pageControl.userInteractionEnabled = false
        // 通过kvc设置图片
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        return pageControl

        
    }()



}

// MARK: -EmjoTabBar代理方法
extension HMEmoticonKeyboardView : HMEmoticonToolBarDelegate {
    
    func switchEmojBtn(target: HMEmoticonToolBar, type: HMEmotionType) {
        
        // 这里拿到单击事件
        // 将collectionView滚动到指定的组就可以了
        let indexPaht = NSIndexPath(forItem: 0, inSection: type.rawValue)
        // 让collectionView滚动到指定的indexPath
        collectionView.scrollToItemAtIndexPath(indexPaht, atScrollPosition: .Left, animated: false)
        
        setupPageControl(indexPaht)
        
    }
}

extension HMEmoticonKeyboardView : UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        // 返回多少大组
        return HMEmoticonTools.sharedTools.allEmoticons.count
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        // 每一组多少行
        return HMEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EMJOCOLLECTIONCELL, forIndexPath: indexPath) as! HMEmjoCell
        
        cell.emoticons = HMEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.row]

        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        // 1. 取到当前collectionView里面可见的两个cell
        let cells = collectionView.visibleCells()
        
        // 2. 查看哪个cell在界面上显示的范围大
        
        if cells.count == 2 {
            // 取到两个cell
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            // 判断这两个cell哪一个在界面上显示得多
            //print("\(firstCell.frame.origin.x),\(lastCell.frame.origin.x)")
            let firstResult = abs(firstCell.frame.origin.x - collectionView.contentOffset.x)
            let lastReulst = abs(lastCell.frame.origin.x - collectionView.contentOffset.x)
            
            //            if firstResult < lastReulst {
            //                print("firstCell显示得多")
            //            }else{
            //                print("lastCell显示得多")
            //            }
            // 取到显示范围大的cell
            let cell = firstResult < lastReulst ? firstCell : lastCell
            // 取到该cell对应的indexPath
            let indexPath = collectionView.indexPathForCell(cell)!
            print("当前显示cell的section:\(indexPath.section)")
            
            // 去选中对应的表情按钮
            emoticonToolBar.selectedButtonWithSection(indexPath.section)
            
            setupPageControl(indexPath)
            
        }
    }

    
}


